#include "common.hpp"
#include "server_TCP.hpp"
#include "HTTP_protocol.hpp"
//#include "daemon.hpp"


void login(const http_request& req, http_response& resp)
{
    std::string text = "echo:> " + req.get_args();

    resp.set_version("HTTP/1.1");//此处就随便设置了
    resp.set_code_and_explanation(200);
    resp.header_push_kv("Content-Type", "text/plain");
    resp.header_push_kv("Content-Length", std::to_string(text.size()));
    resp.get_body_ref() = text;

}




int main(int argc, char* argv[])
{
    //./server_UDP [server_port] [server_ip] //不用传ip, 因为INADDR_ANY
    //./server_UDP [server_port]
    if(argc != 2)
    {
        std::cout << "usage: " << argv[0] << " [server_port]" << std::endl;
        exit(USAGE_ERR);
    }

    //::daemon(0, 0); //系统调用
    //Daemon(0, 0); //自己造的轮子

    // LOG_SWITCH_FILE;

    uint16_t port = static_cast<uint16_t>(std::stoi(argv[1]));

    //1,2,3统一为应用层

    // //1.应用层 -- 针对特定应用的实现 -- 直接实现在http_protocol里了
    // std::unique_ptr<calculate> cal_p = std::make_unique<calculate>();

    //2.表示层 -- 数据格式的转换
    std::unique_ptr<http_protocol> ptl_p = std::make_unique<http_protocol>();
    ptl_p->register_req_service("login", login);

    //3.对话层 -- 建立和断开连接
    std::unique_ptr<server_tcp> st_p = std::make_unique<server_tcp>(port, [&ptl_p](std::shared_ptr<Socket>& sock_p, const Inet_Addr& client)
                                                                          {
                                                                               ptl_p->receive_request_and_send_response(sock_p, client);
                                                                          }
                                                                   );

    
    st_p->start();


    return 0;
}